import { checkZhCodeFn } from '@/utils/formCheck';

import { FormItemRule } from 'naive-ui';

import { t } from '@/i18n';

const getDefaultValueFn = (item) => {
  let value: any = '';

  if (item) {
    const { component = '', componentProps = {}, defaultValue = undefined } = item;

    const { multiple = false, defaultValue: cDefaultValue = undefined } = componentProps;

    if (component) {
      switch (component) {
        case 'NSelect':
          value = multiple ? [] : null;
          break;
        case 'NDatePicker':
          value = defaultValue || cDefaultValue;
          break;
        case 'NTreeSelect':
        case'NTimePicker':
          value = null;
          break;
        case 'NInputNumber':
          value = defaultValue || cDefaultValue;
          break;
        default:
          break;
      }
    }
  }

  return value;
}; // 设置form默认值方法

export const getCompoentPropsFn = (item: any, editDisabledData = false) => {
  let data = {};

  if (item) {
    const {
      componentProps = null,
      component = null,
      editDisabled = false,
      minlength = null,
      maxlength = null,
      type = null,
      options = null,
      disabled = null,
      min = null,
      max = null,
      precision = null,
      showButton = null,
      placeholder = null,
    } = item;

    switch (unref(component)) {
      case 'NRadioGroup':
        data = {
          options: [],
        };
        break;

      case 'NSelect':
        data = {
          filterable: true,
          options: [],
        };
        break;

      case 'NInputNumber':
        data = {
          showButton: false,
        };
        break;

      default:
        data = {
          maxlength: 250,
          clearable: true,
        };
        break;
    }

    if (disabled) {
      data['disabled'] = disabled;
    }

    if (editDisabled) {
      data['disabled'] = editDisabledData;
    }

    if (minlength) {
      data['minlength'] = minlength;
    }

    if (maxlength) {
      data['maxlength'] = maxlength;
    }

    if (type) {
      data['type'] = type;
    }

    if (options) {
      data['options'] = options;
    }

    if (min) {
      data['min'] = min;
    }

    if (max) {
      data['max'] = max;
    }

    if (precision !== null) {
      data['precision'] = precision;
    }

    if (showButton !== null) {
      data['showButton'] = showButton;
    }

    if (placeholder !== null) {
      data['placeholder'] = placeholder;
    }

    if (componentProps) {
      data = {
        ...data,
        ...componentProps,
      };
    }
  }

  return data;
}; // 组装form方法

const getRulesFn = (item: any) => {
  let list: any = [];

  if (item) {
    const {
      required = false,
      rules = [],
      component = null,
      checkZhCode = false,
      requiredRulesType = null,
    } = item;

    if (rules && rules.length) {
      list = rules;
    } else {
      if (checkZhCode) {
        // 不支持中文和空格
        list.push({
          required,
          validator(_rule: FormItemRule, value: string) {
            return checkZhCodeFn(value, true);
          },
          trigger: ['blur'],
        });
      } else {
        if (required) {
          const rulesData = {
            required: true,
            message: t('common.formTipMsg.input'),
            trigger: [
              `${
                [
                  'NSelect',
                  'NRadioGroup',
                  'NTreeSelect',
                  'NRadioButtonGroup',
                  'NCheckbox',
                  'NCheckboxGroup',
                  'NDatePicker',
                  'NMonthPicker',
                  'NRangePicker',
                  'NWeekPicker',
                  'NTimePicker',
                ].includes(component)
                  ? 'change'
                  : 'blur'
              }`,
            ],
          };

          if (requiredRulesType) {
            rulesData['type'] = requiredRulesType;
          }

          list.push(rulesData);
        }
      }
    }
  }

  return list;
}; // 组装form校验规则方法

export const setFormSchemasFn = (formSchemas: any = [], disabled = false) => {
  const list: any = [];

  if (formSchemas && formSchemas.length) {
    for (const item of formSchemas) {
      const {
        label = '',
        prop = '',
        field = '',
        defaultValue = null,
        component = null,
        editHidden = false,
        hidden = null,
      } = item;

      if (label) {
        list.push({
          ...item,
          component: component || 'NInput',
          field: field || prop || label,
          hidden: hidden !== null ? hidden : editHidden ? disabled : false,
          defaultValue:
            defaultValue || ['', 0].includes(defaultValue) ? defaultValue : getDefaultValueFn(item),
          componentProps: getCompoentPropsFn(item, disabled),
          rules: getRulesFn(item),
        });
      }
    }
  }

  return list;
}; // 组装设置form列表方法
